perm filename 106A23[1,RWF] blob sn#724833 filedate 1983-09-23 generic text, type C, neo UTF8
COMMENT āŠ—   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002			The Most Unforgettable Characters I Have Ever Met
C00011 ENDMK
CāŠ—;
		The Most Unforgettable Characters I Have Ever Met



Pascal works with other types of data besides numbers and truth values.  One
such type is the set of characters found on the typica computer terminal
keyboard.  The set includes the digits 0-9, the letters a-z, A-Z i both cases
(minuscule and majuscule, if you want the precise names), all the familiar
punctuation marks, all the mathematical symbols used in Pascal, the space, and
a few other odds and ends like $ and @.

This type is called CHAR (which may as well be pronounced ``character'') in
Pascal.  Like any fully equipped modern type, it has its own citizenry of
constants and variables, and an intra- and inter-type transportation system
of operators and functions, which can be combined in expressions for more
elaborate tours.

For the set of possible values of type CHAR, see Table   .  There is a literal
constant for each one.  With one exception, the literal constant is the
character itself, preceded and followed by apostrophes (`A', `2', `:').  The
exception is the apostrophe itself; its constant form is four apostrophes in
a row ('''').

Symbolic constants can be defined in terms of the literal constants:

	CONST LASTVOWEL=`u'.

Variables are of type CONST if declared so; they may be assigned any values of
of that type.  Here is a fragmentary program using only characters:

	PROGRAM---
	CONST COLON=`:'; MYINITIAL=`F';
	VAR C,H : CHAR;
		BEGIN					B:FAF
		C:=`A';
		H:=MYINITIAL;
		WRITE(`B',COLON,H,C)
		C:=H
		WRITELN(C)
		END

Characters can be read to characters variables from the INPUT file, or from
any text file or CHAR file.  If

	READ(C,H)

appeared in the above program, the next two characters would be read and
assigned to C and H.  At the end of a line, READ(C) would read the end-of-line
symbol (which is not a character in the sense of belonging to CHAR) and assign
a space to C.

The characters of CHAR have a conventional ordering on each computer, which
roughly corresponds to alphabetical order.  There is also a conventional
numbering, giving each character the number of its place in the ordering.
We can use the operators <,>,=, and their combinations to test the relative
ordering of two characters.  Standard Pascal requires that the capital letters
A-Z be in alphabetical order, and that the lower case letters be in alphabetical
order, with no other characters interspersed.  To find out if the value of C,
already known to be a letter, is one of `I',`J',`K',`L',`M', or `N' in either
case, we could use

	IF (C>=`I')AND(C<=`N')OR(C>=`i')AND(C<=`n').

Standard Pascal also requires that the digits 0-9, as characters, be in numerical
order and consecutive.

Here is program fragment which expects the word YES or NO on the INPUT file,
possibly preceded by spaces and abbreviated.  IT will read the whole word,
and choose between commands SY and SN.

	READ(C);
	WHILE C=` ' DO READ(C);
	READ(H);
	WHILE H<> ` ' DO READ(H);
	IF C=`Y' THEN SY
	ELSE IF C=`N' THEN SN
	ELSE WRITE(`INPUT OTHER THAN Y,N')

Standard functions with characters as arguments or as values include these:

	ORD(C) is the ordinal number of the character C.
		Tables          give the most common
		ordinal numberings of characters; you
		should find out which is used on your 
		computer.

	CHR(N)  is the character (if any) whose ordinal
		number is N.  It is the inverse of ORD;
		CHR(ORD(C))=C.

	PRED(C) is the immediate predecessor of C in the
		ordering; it is the same as CHR(ORD(C)-1).
		If C is the first character in the ordering,
		it is a mistake to use PRED(C).

	SUCC(C) is the immediate successor of C in the ordering;
		it is the same as CHR(ORD(C)+1).  If C is the
		last character in the ordering, it is a mistake
		to use SUCC(C).

To print the upper case alphabet, we can use either of the two progrm fragments


(1)	C:=`A';
	WHILE C<=`Z' DO
		BEGIN
		WRITE(C)
		C:=SUCC(C)
		END

(2)	FOR C:=`A' TO `Z' DO
		WRITE(C)

The second program works because iteration variables may be of any type that
has an ordinal numbering, in particular of type CHAR.

We shall later encounter types which have as their values whole sequences of
characters, including words, sentences, formulas, etc; such sequences we call
strings.  We have already encountered writing of string constants, consisting
of two or more characters enclosed in single quotes (apostrophes).  For the
moment, the reader may treat writing of a string constant as an abbreviation
for writing the characters of the constant;

	WRITE(`ABC') abbreviates WRITE(`A',`B',`C').

What can be done with characters?  Only a limited amount, until we can work
with long strings of them.  As an example above showed, they can be used as
convenient input codes to choose between actions.  In output, strings of
characters provide titles, punctuation, etc.  Within a computation, however,
characters are only a convenience; rather than work with characters, a program
could work with their ordinal numbers, so we can not expect to do anything with
characters we could not do without them.

	Exercise: Write an iterative program to print the consonants.

	Exercise: Write a program to read a roman numeral and print its
		value in decimal.
		(Hint: the value of letter is negative if the next
		letter has a larger value.)

	Exercise: Write a program to count to M in roman numerals.